
function [Xm] = ConvDaytoMonth(X)
% Converts daily data to month data by averaging daily values
% Date in first colum and series in second 
% UJJ march 2024

% Convert the dates to date format
dates = datetime(X(:, 1), 'ConvertFrom', 'datenum');

% Find the unique months in the dataset
months = unique(dates.Year*100 + dates.Month);

% Initialize matrix Xm to store monthly averages
Xm = zeros(length(months), 2);

% Calculate monthly averages
for i = 1:length(months)
    % Filter prices for the current month
    prices_month = X(dates.Year*100 + dates.Month == months(i), 2);
    
    % Calculate the average price for the month
    Xm(i, 1) = datenum(year(dates(1)), month(dates(1)) + i - 1, 1); % First day of the month
    Xm(i, 2) = mean(prices_month);
end

%Remove first and last months, which are (very likely) not complete
Xm(1,:)=[];
Xm(end,:) = [];

end

